$(document).ready(function() {

    $("#main_content_title").html("用户管理")

    $('#dashboard').dashboard({ draggable: true });




    var zTreeObj;
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    var setting = {
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: null
            }
        },
        view: { //表示tree的显示状态
            selectMulti: false //表示禁止多选
        },
        check: { //表示tree的节点在点击时的相关设置
            enable: false, //是否显示radio/checkbox
            chkStyle: "checkbox", //值为checkbox或者radio表示
            checkboxType: { p: "", s: "" }, //表示父子节点的联动效果
            radioType: "level" //设置tree的分组
        }
    };
    // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
    var zNodes = [];
    zTreeObj = $.fn.zTree.init($("#department_tree"), setting, zNodes);

    var zdata = [];
    DeptGetAll(function(data) {
        for (var i = 0; i < data.length; i++) {
            var m = data[i];
            var zdataItem = {};
            zdataItem["id"] = m.id;
            zdataItem["pId"] = null;
            zdataItem["name"] = m.organname;
            zdataItem["children"] = addobj(m);
            zdata.push(zdataItem);
        }
        // zTreeObj.updateNode(zdata);
        $.fn.zTree.init($("#department_tree"), setting, zdata)
            //zTreeObj.refresh();
        console.log(zdata)
    }, function(error) {

    });



    $('.datatable').datatable({
        checkable: false,
        sortable: false,
        data: {
            cols: [{
                width: 10,
                text: '序号',
                type: 'number',
                flex: false,
                colClass: 'text-center'
            }, {
                width: 30,
                text: '姓名',
                type: 'string',
                flex: false,
                sort: 'down',
                colClass: 'text-center'
            }, {
                width: 40,
                text: '工号',
                type: 'string',
                flex: true,
                colClass: 'text-center'
            }, {
                width: 80,
                text: '手机号',
                type: 'string',
                flex: false,
                sort: 'down',
                colClass: 'text-center'
            }, {
                width: 80,
                text: '角色',
                type: 'string',
                flex: true,
                colClass: 'text-center'
            }, {
                width: 160,
                text: '操作',
                type: 'string',
                flex: true,
                colClass: 'text-center'
            }],
            rows: [{
                checked: false,
                data: [1, '黎明', '15159', '13505159275', '普通用户', '<a href="#" data-toggle="modal" data-target="#myModal"><i class="icon icon-edit"></i> 修改</a><a href="#"><i class="icon icon-remove-circle"></i> 删除</a>']
            }, {
                checked: false,
                data: [1, '黎明', '15159', '13505159275', '普通用户', '<a href="#" data-toggle="modal" data-target="#myModal"><i class="icon icon-edit"></i> 修改</a><a href="#"><i class="icon icon-remove-circle"></i> 删除</a>']
            }, {
                checked: false,
                data: [1, '黎明', '15159', '13505159275', '普通用户', '<a href="#" data-toggle="modal" data-target="#myModal"><i class="icon icon-edit"></i> 修改</a><a href="#"><i class="icon icon-remove-circle"></i> 删除</a>']
            }, {
                checked: false,
                data: [1, '黎明', '15159', '13505159275', '普通用户', '<a href="#" data-toggle="modal" data-target="#myModal"><i class="icon icon-edit"></i> 修改</a><a href="#"><i class="icon icon-remove-circle"></i> 删除</a>']
            }, {
                checked: false,
                data: [1, '黎明', '15159', '13505159275', '普通用户', '<a href="#" data-toggle="modal" data-target="#myModal"><i class="icon icon-edit"></i> 修改</a><a href="#"><i class="icon icon-remove-circle"></i> 删除</a>']
            }, {
                checked: false,
                data: [1, '黎明', '15159', '13505159275', '普通用户', '<a href="#" data-toggle="modal" data-target="#myModal"><i class="icon icon-edit"></i> 修改</a><a href="#"><i class="icon icon-remove-circle"></i> 删除</a>']
            }, {
                checked: false,
                data: [1, '黎明', '15159', '13505159275', '普通用户', '<a href="#" data-toggle="modal" data-target="#myModal"><i class="icon icon-edit"></i> 修改</a><a href="#"><i class="icon icon-remove-circle"></i> 删除</a>']
            }]
        }
    });



});


function addobj(data) {
    var zdata = [];
    if (typeof(data.childs) != "undefinde" && data.childs != null &&
        data.childs.length > 0) {
        for (var i = 0; i < data.childs.length; i++) {
            var m = data.childs[i];
            var zdataItem = {};
            zdataItem["id"] = m.id;
            zdataItem["pId"] = data.id;
            zdataItem["icon"] = "";
            zdataItem["name"] = m.organname;
            zdataItem["children"] = addobj(m);
            zdata.push(zdataItem);
        }
        return zdata;
    } else {
        return [];
    }
}